/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.reports;

import com.railway.reports.utils.CurrencyType;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.dynamicreports.examples.Templates;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import static net.sf.dynamicreports.report.builder.DynamicReports.*;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.group.ColumnGroupBuilder;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.constant.VerticalAlignment;
import net.sf.dynamicreports.report.datasource.DRDataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;

/**
 *
 * @author Kenny
 */
public class TicketReport {

    private JRViewer reportView;
    private JRDataSource datasource;

    public void buildData(ArrayList<Map<String, Object>> reportData) {
        datasource = buildDataSource(reportData);
    }

    public JRViewer buildReport() {
        try {
            CurrencyType currencyType = new CurrencyType();
            StyleBuilder titleStyle = stl.style(Templates.boldCenteredStyle)
                    .setVerticalAlignment(VerticalAlignment.MIDDLE)
                    .setFontSize(13);
            JasperReportBuilder report = new JasperReportBuilder();
            TextColumnBuilder monthCol = col.column("Month", "month", type.dateYearToMonthType());
            TextColumnBuilder referenceCol = col.column("Reference No", "referenceNo", type.stringType());
            TextColumnBuilder datePurchasedCol = col.column("Date Purchased", "datePurchased", type.dateType());
            TextColumnBuilder departureDateCol = col.column("Departure Date", "departureDate", type.dateType());
            TextColumnBuilder originCol = col.column("Origin", "origin", type.stringType());
            TextColumnBuilder destinationCol = col.column("Destination", "destination", type.stringType());
            TextColumnBuilder classCol = col.column("Train Class", "class", type.stringType());
            TextColumnBuilder fareCol = col.column("Fare", "fare", currencyType);
            TextColumnBuilder statusCol = col.column("Status", "status", type.stringType());
            report.setTemplate(Templates.reportTemplate).columns(
                    monthCol,
                    referenceCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    datePurchasedCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    departureDateCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    originCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    destinationCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    classCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    fareCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    statusCol.setHorizontalAlignment(HorizontalAlignment.CENTER))
                    .title(cmp.horizontalList().add(
                    cmp.image(this.getClass().getResourceAsStream("/com/railway/image/logo.png")).setFixedDimension(192, 70),
                    cmp.text("Ticket Report").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.LEFT),
                    cmp.text(new Date()).setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.RIGHT)).newRow()
                    .add(cmp.filler().setStyle(stl.style().setTopBorder(stl.pen2Point())).setFixedHeight(10)))
                    .pageFooter(cmp.pageXofY());
            ColumnGroupBuilder itemGroup = grp.group(monthCol);
            itemGroup.setPrintSubtotalsWhenExpression(
                    exp.printWhenGroupHasMoreThanOneRow(itemGroup));
            report.subtotalsAtSummary(sbt.sum(fareCol)).subtotalsAtFirstGroupFooter(sbt.sum(fareCol));
            report.groupBy(itemGroup);
            report.setDataSource(datasource);
            JasperPrint jsp = report.toJasperPrint();
            reportView = new JRViewer(jsp);
            reportView.setZoomRatio(0.8f);
            return reportView;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private JRDataSource buildDataSource(ArrayList<Map<String, Object>> data) {
        DRDataSource ds;
        SimpleDateFormat format = new SimpleDateFormat("MMMMM yyyy");
        ds = new DRDataSource("month", "referenceNo", "datePurchased", "departureDate", "origin", "destination", "class", "fare", "status");
        for (int i = 0; i < data.size(); i++) {
            try {
                Map<String, Object> map = data.get(i);
                ds.add(
                        format.parse(format.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("datePurchased").toString()))),
                        map.get("referenceNo"),
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("datePurchased").toString()),
                        new SimpleDateFormat("yyyy-MM-dd").parse(map.get("departureDate").toString()),
                        map.get("origin"),
                        map.get("destination"),
                        map.get("class"),
                        new BigDecimal(map.get("fare").toString()),
                        map.get("status"));
            } catch (ParseException ex) {
                Logger.getLogger(TicketReport.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return ds;
    }
}
